import {Component, ViewChild} from "@angular/core";
import {Content, IonicPage, NavController, Refresher} from "ionic-angular";
import {HttpClient} from "@angular/common/http";
import {AppUrl} from "../../../app/app.general";
import {PageSplit} from "../../utils/page/page";
import {CommonService} from "../../../app/common.service";

@IonicPage()
@Component({
  templateUrl:'business-food-print.html',
  selector:'business-food-print'
})

export class BusinessFoodPrintPage{

  @ViewChild(Content) content: Content;
  @ViewChild(Refresher) refresher: Refresher;

  private page: PageSplit = {
    pageSize: 5,
    pageNo: 0,
    isLastPage: false
  };

  tabelPrint:Array<{orderno:string,tableNo:string,printStat:string,printtime:string,tableOrder:Array<{food:string,num:any}>}>;
  constructor(public navCtrl:NavController,public http:HttpClient,private commonService:CommonService){
    this.tabelPrint=[];
  }
  ionViewWillEnter(){
    this.refresher._top = '50px';
    this.refresher.state = 'refreshing';
    this.refresher._beginRefresh();
    // this.doRefresh();
  }

  getdata(refresher?:any){
    this.page.pageNo++;

    this.http.post(AppUrl.food.print,{pageNo:this.page.pageNo,pageSize:this.page.pageSize}).subscribe(data=>{
      if(refresher){
        this.refresher.complete();
        this.tabelPrint=[];
      }

      console.log(data);
      let temp1:any=data;
      let temp:any=temp1.data;
      for(let ar of temp[0]){
        let index=this.tabelPrint.findIndex(e=>e.printtime==ar.printtime);
        if(index!=-1){
          if(this.tabelPrint[index].orderno==ar.orderno){
            this.tabelPrint[index].tableOrder.push({
              food:ar.foodname,
              num:ar.num
            });
          }else{
            this.tabelPrint.push({
              orderno:ar.orderno,
              tableNo:ar.tableno,
              printStat:ar.printflag=='1'?'已打印':'未打印',
              printtime:ar.printtime,
              tableOrder:[{
                food:ar.foodname,
                num:ar.num
              }]
            });
          }

        }else{
          this.tabelPrint.push({
            orderno:ar.orderno,
            tableNo:ar.tableno,
            printStat:ar.printflag=='1'?'已打印':'未打印',
            printtime:ar.printtime,
            tableOrder:[{
              food:ar.foodname,
              num:ar.num
            }]
          });
        }
      }
      this.page.isLastPage = temp[1]==0?true:false;
      // console.log("iLength:"+this.iLength);

    },reject=>{
      this.commonService.httpError(reject);
      this.refresher.complete();
    });
  }
  doInfinite(infiniteScroll):Promise<any>{
    return new Promise((resolve)=>{
      let that=this;
      setTimeout(function () {
        if(!that.page.isLastPage){
          that.getdata();
          resolve();
        }else{
          resolve();
        }
      },1000);
    });
  }

  doRefresh(){
    this.page = {
      isLastPage: false,
      pageSize: 5,
      pageNo: 0
    };
    this.getdata(this.refresher);
  }

}
